package org.example.erpapiservicespringpgimpl;

import org.example.erpapicommon.Util.BasePage;
import org.example.erpapicommon.Util.Pageable;
import org.example.erpapicommon.Util.Param.OrderQueryParam;
import org.example.erpapicommon.model.Order;
import org.example.erpapicommon.model.TOrder;
import org.example.erpapicommon.model.TOrderDetail;
import org.example.erpapicommon.service.OrderService;
import org.example.erpapiservicespringpgimpl.Dao.OrderDao;
import org.example.erpapiservicespringpgimpl.entity.OrderDetailEntity;
import org.example.erpapiservicespringpgimpl.entity.OrderEntity;
import org.example.erpapiservicespringpgimpl.mapper.OrderMapper;
import org.example.erpapiservicespringpgimpl.mapper.OrderPageMapper;
import org.seasar.doma.jdbc.SelectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
public class OrderServicePgImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderPageMapper orderPageMapper;
    @Override
    @Cacheable(value = "erp_api",key = "'pageOrders_'+#param.toString()", condition = "#param != null")
    public Pageable<TOrder> getOrders(OrderQueryParam param) {
        SelectOptions options = SelectOptions.get().count();
        List<OrderEntity> orderList = orderDao.PageOrders(param, options);
        int totalCount = (int) options.getCount();
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        int totalPage = PageUtil.getTotalPage((int) totalCount, pageSize);
        int offset = PageUtil.getOffset(pageNumber, pageSize);
        options.offset(offset).limit(pageSize);
        List<OrderEntity> orderEntityList1 = orderDao.PageOrders(param, options);
        List<TOrder> TOrderList = orderPageMapper.fromOrderEntitiesToOrders(orderEntityList1);
        Pageable<TOrder> TOrderPageable = new BasePage<>(TOrderList, pageNumber, pageSize,totalPage,  totalCount);
        return TOrderPageable;

    }

    @Override
    @Cacheable(value = "erp_api",key = "'findOrderisByid_'+#id")
    public Optional<Order> getOrdersById(String id) {
        Optional<OrderEntity> orderOptional = orderDao.getOrdersById(id);
        if (orderOptional.isEmpty()) {
            return Optional.empty();
        }
        List<TOrderDetail> tOrderDetailList =getOrderDetailByOrderId(id);
        return orderOptional.map(orderEntity -> {
           Order order = orderMapper.fromOrderEntitiesToOrder(orderEntity);
            int totalSale=0;
            for (TOrderDetail tOrderDetail:tOrderDetailList){
                totalSale+=tOrderDetail.getQuantity()*tOrderDetail.getActualUnitPrice().intValue();
            }
           order.setTotalPrice(totalSale);
           order.setOrderDetails(tOrderDetailList);
           return order;
        });
    }

    @Override
    public List<TOrderDetail> getOrderDetailByOrderId(String OrderId) {
        List<OrderDetailEntity> orderDetailEntities = orderDao.getOrderDetailByOrderId(OrderId);
        return orderPageMapper.fromOrderDetailEntitiesToTOrderDetails(orderDetailEntities);
    }
}
